﻿/*
    AccessHelper static members
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace DbHelp
{
    public partial class AccessHelper
    {
        static string get_defualt_dbpath()
        {
            return Path.GetDirectoryName(Assembly.GetAssembly(typeof(AccessHelper)).Location) + "\\PiView.mdb";
        }

        static string get_defualt_dbpath(string pPath)
        {

            if (string.IsNullOrEmpty(pPath))
            {
                return Path.GetDirectoryName(Assembly.GetAssembly(typeof(AccessHelper)).Location) + "\\PiView.mdb";
            }
            return pPath;
        }


        public static string mPiewDb = AccessHelper.IniReadValue("SERVER", "MDBPATH", AppDomain.CurrentDomain.BaseDirectory + @"\sysconfig.ini");



        public static string mReportDb = AccessHelper.IniReadValue("SERVER", "MDBPATH_REPORT", AppDomain.CurrentDomain.BaseDirectory + @"\sysconfig.ini");

        public static string mPiViewPath = AccessHelper.IniReadValue("SERVER", "PIVIEW_PATH", AppDomain.CurrentDomain.BaseDirectory + @"\sysconfig.ini");
        public static string mPiViewArges = AccessHelper.IniReadValue("SERVER", "PIVEW_ARGES", AppDomain.CurrentDomain.BaseDirectory + @"\sysconfig.ini");


        public static byte[] f_strToByte(string pStr)
        {
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(pStr);
            return byteArray;
        }


        public static string f_ByteToStr(byte[] byteArray)
        {
            string str = System.Text.Encoding.UTF8.GetString(byteArray);
            return str;
        }


     




        #region API (INI)

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        #endregion


        /// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="Section">项目名称(如 [TypeName] )</param>
        /// <param name="Key">键</param>
        /// <param name="Value">值</param>
        public static void IniWriteValue(string Section, string Key, string Value, string FileName)
        {
            WritePrivateProfileString(Section, Key, Value, FileName);
        }

        /// <summary>
        /// 读出INI文件
        /// </summary>
        /// <param name="Section">项目名称(如 [TypeName] )</param>
        /// <param name="Key">键</param>
        public static string IniReadValue(string Section, string Key, string FileName)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, FileName);
            return temp.ToString();
        }


    }
}
